<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="ctx" value="${pageContext.request.contextPath}" />

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8"> 
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> 
  <title>乐直播-硬件管理</title> 
  <link rel="stylesheet" href="http://cdn.lezhibo.com/static/layui/css/layui.css?cdnv=0.16"> 
  <link rel="stylesheet" href="http://cdn.lezhibo.com/static/portal/css/le-main24.css?cdnv=0.16"> 
  <link rel="stylesheet" href="http://cdn.lezhibo.com/static/libs/font-awesome/css/font-awesome.min.css?cdnv=0.16"> 
  <style type="text/css">
    .le-live-list {
        margin: 5px;
        width: 150px;
        height: 150px;
        position:relative;
    }
    .le-live-list .live-list-image {
        height: 100px;
        margin: 0 auto;
        overflow: hidden;
        text-align: center;
        width: 100%;
    }
    .le-live-list-p{line-height: 28px;margin: 6px 10px 0 0;}
    .layui-textarea { width: 100%;}
    .le-word-tip{
        margin-left: 30px;
        color: #999;
        font-size: 8px;
    }
        </style> 
 </head> 
 <body> 
  <form id="form" class="layui-form le-layer-from" action="/portal/activity/doCreate" style="padding-bottom: 1rem"> 
   <!--<input type="text" name="name" lay-verify="title" autocomplete="off" placeholder="直播间名称" class="layui-input">--> 
   <div class="layui-form-item"> 
    <label class="layui-form-label">活动名称</label> 
    <div class="layui-input-block"> 
     <input type="text" name="name" validate="empty" lay-verify="title" autocomplete="off" placeholder="请输入活动名称" class="layui-input"> 
    </div> 
    <p class="tips" style="color: #999;margin-left: 95px; ">提示：鉴于微信最多显示26个字，避免显示不全，建议设置不要超过26个字的标题</p> 
   </div> 
   <div class="layui-form-item"> 
    <label class="layui-form-label">活动简介</label> 
    <div class="layui-input-block"> 
     <textarea class="layui-textarea " name="content" validate="empty" id="LAY_demo_editor" placeholder="请输入活动简介"></textarea> 
    </div> 
    <p class="tips" style="color: #999;margin-left: 95px; ">提示：鉴于微信最多显示45个字，避免显示不全，建议设置不要超过45个字的标题</p> 
   </div> 
   <div class="layui-form-item"> 
    <label class="layui-form-label">活动公告</label> 
    <div class="layui-input-block"> 
     <textarea class="layui-textarea " name="notice" validate="empty" placeholder="请输入活动公告"></textarea> 
    </div> 
    <p class="tips" style="color: #999;margin-left: 95px; ">提示：可输入多条活动公告，每条公告之间采用回车分隔，鉴于前台显示效果建议每条活动公告不要超过22个字</p> 
   </div> 
   <div class="layui-form-item"> 
    <label class="layui-form-label">所属直播间</label> 
    <div class="layui-input-block"> 
     <select lay-filter="liveroomId" name="liveroomId" validate="empty"> <option>选择直播间</option> <option value="d9c6afa2e0b649afbd56d3c10e6ffc3d">体验303直播间(体验303直播间)</option> </select> 
    </div> 
   </div> 
   <div class="layui-form-item activityCategoryList" style="display: none" pane=""> 
    <label class="layui-form-label">活动分类</label> 
    <div class="layui-input-block activityCategory"> 
    </div> 
   </div> 
   <div class="layui-form-item"> 
    <label class="layui-form-label">封面图</label> 
    <div class="layui-input-block"> 
     <div class="le-hardware-layer-image" style="width: 180px;"> 
      <img class="le-avatar" src="http://cdn.lezhibo.com/static/portal/images/activity-default.jpg"> 
     </div> 
     <div class="le-hardware-layer-upload"> 
      <input type="file" name="file" class="layui-upload-file" lay-title="选择"> 
      <input type="hidden" class="avatar" name="thumbnail"> 
     </div> 
    </div> 
    <div class="tips" style="color: #999;margin-left: 95px; padding-top: 100px">
     提示：建议上传分辨率为720*480，大小2M以内，格式为PNG、JPG、JPEG等常用格式图片
    </div> 
   </div> 
   <div class="live-box"> 
    <div class="layui-form-item le-live-item layui-show "> 
     <label class="layui-form-label le-lebel">复制上个活动插件配置</label> 
     <div class="layui-input-block"> 
      <input type="checkbox" name="addon-memory-enable" value="true" lay-skin="switch" lay-filter="switchAddonMemory" checked title="开关"> 
     </div> 
    </div> 
    <div class="tips" style="color: #999;margin-left: 95px; padding-top: 10px">
     提示：启用该功能后此活动自动复制上一个活动的所有插件配置
    </div> 
   </div> 
   <div class="live-box portalActivityVideo" style="display: none"> 
    <div class="layui-form-item le-live-item layui-show "> 
     <label class="layui-form-label le-lebel">是否设置成门户视频</label> 
     <div class="layui-input-block"> 
      <input type="checkbox" name="isPortalActivity" value="true" lay-skin="switch" lay-filter="switchAddonMemory" title="开关"> 
     </div> 
    </div> 
    <input type="hidden" name="portalId"> 
    <div class="tips" style="color: #999;margin-left: 95px; padding-top: 10px">
     提示：设置成门户视频，改活动将在门户展现
    </div> 
   </div> 
   <div class="layui-form-item portalActivityVideo" style="display: none" pane=""> 
    <label class="layui-form-label">活动分类</label> 
    <div class="layui-input-block portalCategory"> 
    </div> 
   </div> 
   <!--<div class="layui-form-item" pane="">--> 
   <!--<label class="layui-form-label">原始复选框</label>--> 
   <!--<div class="layui-input-block">--> 
   <!--<input type="checkbox" name="like1[write]" lay-skin="primary" title="写作" checked="">--> 
   <!--<input type="checkbox" name="like1[read]" lay-skin="primary" title="阅读">--> 
   <!--<input type="checkbox" name="like1[game]" lay-skin="primary" title="游戏" disabled="">--> 
   <!--</div>--> 
   <!--</div>--> 
  </form> 
  <div class="le-hardware-layer-btn"> 
   <button id="btnSubmit" type="button" class="layui-btn" onclick="doCreate()">确定</button> 
  </div> 
  <script type="text/javascript" src="http://cdn.lezhibo.com/static/libs/jquery/jquery.min.js?cdnv=0.16"></script> 
  <script type="text/javascript" src="http://cdn.lezhibo.com/static/libs/jquery/jquery.form.min.js?cdnv=0.16"></script> 
  <script type="text/javascript" src="http://cdn.lezhibo.com/static/layui/layui.js?cdnv=0.16"></script> 
  <script type="text/javascript" src="http://cdn.lezhibo.com/static/layui/lay/dest/layui.all.js?cdnv=0.16"></script> 
  <script>

        var cdnPath="http://cdn.lezhibo.com";//cdn路径
        function openLayer(url, title, func) {
            parent.openLayer(url,title,func);
        }
        function layerInit() {
            $("[layer-href]").click(function () {
                openLayer($(this).attr("layer-href"), $(this).attr("layer-title"), $(this).attr("layer-func"))
            });
        }

        $("[confirm-href]").click(function () {
            var href = $(this).attr("confirm-href");
            if ((typeof(href) == "undefined") || href == null || href == "") {
                return;
            }

            var title = $(this).attr("confirm-title");
            if ((typeof(title) == "undefined") || title == null || title == "") {
                title = "您确定执行该操作吗？"
            }
            layer.confirm(title, {icon: 9, title: '确定操作 ？'}, function (index) {
                        layer.close(index);
                        $.get(href, function (result) {
                            if (result.isOk) {
                                location.reload();
                            } else {
                                alert(result.message);
                            }
                        });
                    },
                    function (index) {
                        layer.close(index);
                    });
        });

        var verification ={
            mobiel:function(){
                $mobile = $("input[validate='mobile']").val();
                var re = /^1\d{10}$/;
                if((typeof($mobile)!="undefined") && !re.test($mobile)){
                    $("input[validate='mobile']").after('<p class="le-tips"><span style="color: red;width: 100%">请输入正确的手机号</span></p>');
                    return false;
                }
            },
            empty:function(){
                var $input =$("input[validate='empty']");
                for(var i=0;i<$input.length;i++){
                    if($input.eq(i).val().trim().length==0){
                        $("input[validate='empty']").eq(i).after('<p class="le-tips"><span style="color: red;width: 100%">输入不能为空</span></p>');
                        return false;
                    }
                }

                var $textarea =$("textarea[validate='empty']");
                for(var i=0;i<$textarea.length;i++){
                    if($textarea.eq(i).val().trim().length==0){
                        $("textarea[validate='empty']").eq(i).parent().after('<p class="le-tips"><span style="color: red;width: 100%">输入不能为空</span>/p>');
                        return false;
                    }
                }

                var $select =$("select[validate='empty']");
                for(var i=0;i<$select.length;i++){
                    if($select.eq(i).val().trim().length==0){
                        $("select[validate='empty']").eq(i).parent().after('<p class="le-tips"><span style="color: red;width: 100%">输入不能为空</span></p>');
                        return false;
                    }
                }
            },
            email:function(){
                $email=$("input[validate='email']").val();
                var re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
                if((typeof($email)!="undefined") && !re.test($email)){
                    $("input[validate='email']").after('<span class="le-tips"><span style="color: red;width: 100%">请输入正确的邮箱</span></p>');
                    return false;
                }
            },
            password: function () {
                $password = $("input[validate='password']").val();
                var re = /(.+){6,12}$/;
                if ((typeof($password) != "undefined") && !re.test($password)) {
                    $("input[validate='password']").after('<p class="le-tips"><span style="color: red;width: 100%">请输入6到12位密码</span></p>');
                    return false;
                }
            },
            //正整数
            positiveInteger: function () {
                $positiveInteger = $("input[validate='positiveInteger']");
                var re=/^[1-9]\d*|0$/;
                for (var i = 0; i < $positiveInteger.length; i++) {
                    var every=$positiveInteger.eq(i).val().trim();
                    if ((typeof(every) != "undefined") && !re.test(every)) {
                        $("input[validate='positiveInteger']").eq(i).after('<p class="le-tips"><span style="color: red">请输入一个正整数或者0</span></p>');
                        return false;
                    }
                }
            },
            decimal: function (value) {
                $decimal = $("input[validate='decimal']");
                var re = /^[0-9]+\.{0,1}[0-9]{0,2}$/;
                for (var i = 0; i < $decimal.length; i++) {
                    var every=$decimal.eq(i).val().trim();
                    if ((typeof(every) != "undefined") && !re.test(every)) {
                        $("input[validate='decimal']").eq(i).after('<p class="le-tips"><span style="color: red">值精确到0.01,不能小于0</span></p>');
                        return false;
                    }
                }
            }
         };


        var regularVerify = {
            mobile: function (value) {
                var re = /^1\d{10}$/;
                if ((typeof(value) != "undefined") && !re.test(value)) {
                    return false;
                }
            },
            email: function (value) {
                var re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
                if ((typeof(value) != "undefined") && !re.test(value)) {
                    return false;
                }
            },
            password: function (value) {
                var re = /(.+){6,12}$/;
                if ((typeof(value) != "undefined") && !re.test(value)) {
                    return false;
                }
            },
            integer: function (value)     {
//                    var re = /^[1-9]\d*$/;
                var re = /^\d+$/;
                if ((typeof(value) != "undefined") && !re.test(value)) {
                    return false;
                }
            },
            decimal: function (value) {
//                    var re = /^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$/;
                var re = /^[0-9]+\.{0,1}[0-9]{0,2}$/;
                if ((typeof(value) != "undefined") && !re.test(value)) {
                    return false;
                }
            },
            empty: function (value) {
                if (value.trim().length == 0) {
                    return false;
                }
            }
        }


        function leSubmit(){
        $("[le-submit]").click(function(){
            var sfunc = $("#form").attr("success-func");
            var bfunc = $("#form").attr("before-func");
            $("#form").ajaxSubmit({
                type:"post",
                dataType:"json",
                beforeSend:function(){
                    if(bfunc != null){
                        if(false == eval(bfunc)){
                            return false;
                        }
                    }
                    $(".le-tips").remove();
                    if(false == verification.empty()){
                        return false;
                    }
                    if(false == verification.mobiel()){
                        return false;
                    }
                    if(false == verification.email()){
                        return false;
                    }
                    if(false == verification.password()){
                        return false;
                    }
                    if(false == verification.positiveInteger()){
                        return false;
                    }
                },
                success:function(result){
                    if(sfunc != null){
                        eval(sfunc);
                        return;
                    }

                    if(result.isOk){
                        parent.isUpdateData = true;
                        parent.layer.closeAll();
                    }else{
                        layer.alert(result.message);
                    }
                },
                error:function(){
                    layer.alert("信息提交错误");
                }
            });
        })
}

            layerInit();
        leSubmit();
    //<script>

    layui.upload({
        url: '/portal/attachment/upload',
        success: function(result){
            if(result.isOk){
                $(".avatar").val(result.path);
                $(".le-avatar").attr("src",result.cdnPath);
            }else{alert(result.message);}
        },
        error:function(){
            alert("信息提交错误");
        }
    });

    //查询分类
    var form = layui.form();
    form.on('select(liveroomId)', function(data){
        $.ajax({
            type: 'POST',
            url: '/portal/activity/whichCategory',
            beforeSend:function(){},
            data: {"liveroomId":data.value},
            success:function(ret){ //提交成功的回调函数
                if(ret.isOk){
                    $(".activityCategoryList").show();
                    $("input[name='activityCategoryId']").val(ret.activityCategoryId);
                    var html="";
                    for(var i=0;i<ret.list.length;i++){
                        ret.list[i].title
                        html+=' <input type="checkbox" name="activityCategories" value="'+ret.list[i].id+'" title="'+ret.list[i].title+'" lay-skin="primary">';
                    }
                    $(".activityCategory").html(html);
                    form.render('checkbox');
                }else{
                    $(".activityCategoryList").hide();
                    $("input[name='activityCategoryId']").val("");
                    $(".activityCategory").html("");
                }
            },
            error : function(ret) {
                alert("活动出错");
            }
        });
    });


    function success(result){
        if(result.isOk){
            parent.isUpdateData = true;
            parent.layer.closeAll();
        }else{
            alert(result.message);
        }
    }




    function doCreate() {
        var $form = $("#form");
        $form.ajaxSubmit({
            type: "post",
            dataType: "json",
            beforeSubmit: function(){
                if($("input[name='name']").val().trim().length==0){
                    alert("活动名称不能为空!");
                    return false;
                }
                if($("textarea[name='content']").val().trim().length==0){
                    alert("活动简介不能为空!");
                    return false;
                }
                var select = $("select[name='liveroomId']");
                if(select && select.val()){
                    if(select.val().indexOf("选择")>=0){
                        alert("请选择直播间!");
                        return false;
                    }
                }
                $("#btnSubmit").attr("class","layui-btn layui-btn-disabled");
                $("#btnSubmit").attr('onclick','javascript:void(0);');
            },
            success: function (result) {
                if (result.isOk) {
                    alert(result.object);
                    parent.isUpdateData = true;
                    parent.layer.closeAll();
                }else{
                    alert(result.message);
                    $("#btnSubmit").attr("class","layui-btn");
                    $("#btnSubmit").attr("onclick","doCreate()");
                }
            },
            error: function (result) {
                alert("信息提交错误");
                $("#btnSubmit").attr("class","layui-btn");
                $("#btnSubmit").attr("onclick","doCreate()");
            }
        });
    }



            var form = layui.form();
            form.on('select(liveroomId)', function(data){
                $.ajax({
                    type: 'POST',
                    url: '/portal/activity/whichLiveroom',
                    beforeSend:function(){},
                    data: {"liveroomId":data.value},
                    success:function(ret){ //提交成功的回调函数
                        if(ret.isOk){
                            $(".portalActivityVideo").show();
                            $("input[name='portalId']").val(ret.portalId);
                            var html="";
                                for(var i=0;i<ret.portalActivityCategoryVoList.length;i++){
                                    ret.portalActivityCategoryVoList[i].title
                                    html+=' <input type="checkbox" name="portalActivityCategories" value="'+ret.portalActivityCategoryVoList[i].id+'" title="'+ret.portalActivityCategoryVoList[i].title+'" lay-skin="primary">';
                                }
                            $(".portalCategory").html(html);
                            form.render('checkbox');
                        }else{
                            $(".portalActivityVideo").hide();
                            $("input[name='portalId']").val("");
                            $(".portalCategory").html("");
                        }
                    },
                    error : function(ret) {
                       alert("活动出错");
                    }
                });
            });

            //查询分类
    var form1 = layui.form();
    form1.on('select(liveroomId)', function(data){
        //alert(data.value);
        $.ajax({
            type: 'POST',
            url: '/portal/activity/whichCategory',
            beforeSend:function(){},
            data: {"liveroomId":data.value},
            success:function(ret){ //提交成功的回调函数
                if(ret.isOk){
                    $(".activityCategoryList").show();
                    $("input[name='activityCategoryId']").val(ret.activityCategoryId);
                    var html="";
                    for(var i=0;i<ret.list.length;i++){
                        ret.list[i].title
                        html+=' <input type="checkbox" name="activityCategories" value="'+ret.list[i].id+'" title="'+ret.list[i].title+'" lay-skin="primary">';
                    }
                    $(".activityCategory").html(html);
                    form.render('checkbox');
                }else{
                    $(".activityCategoryList").hide();
                    $("input[name='activityCategoryId']").val("");
                    $(".activityCategory").html("");
                }
            },
            error : function(ret) {
                alert("活动出错");
            }
        });
    });

    //</script>    
 </body>
</html>